/*
 * Copyright (C) GSX Techedu Inc. All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 */

package com.nime.novel.crawl.core.list.impl;

import com.google.common.collect.Lists;
import com.nime.novel.crawl.constant.ChannelEnum;
import com.nime.novel.crawl.constant.StatusEnum;
import com.nime.novel.crawl.core.list.UrlGenService;
import com.nime.novel.crawl.domain.CrawlUrl;
import com.nime.novel.crawl.utils.UrlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liujialiang
 * @description
 * @team wuhan operational dev.
 * @date 2020/8/28 11:30 下午
 **/
@Slf4j
@Service
public class ChapterUrlGenServiceImpl extends UrlGenService {

    @Override
    protected CrawlUrl getNewestUrl(Integer policyId) {
        CrawlUrl crawlUrl = crawlUrlService.getOldCrawlUrlByStatus(policyId, ChannelEnum.CHAPTER_LIST.getCode(), StatusEnum.TODO);
        if (null == crawlUrl) {
            log.info("频道更新完毕: channel={}", ChannelEnum.CHAPTER.getCode());
            return null;
        }
        return crawlUrl;
    }

    @Override
    protected List<String> parseUrl(String html) {
        List<String> urls = Lists.newArrayList();
        Document doc = Jsoup.parse(html);
        Elements chapterEls = doc.select("div#chapterlist > p > a");
        if (chapterEls.size() == 0) {
            return urls;
        }
        for (Element element : chapterEls) {
            String suffix = element.attr("href");
            if (StringUtils.isNotEmpty(suffix) && suffix.endsWith(".html")) {
                urls.add(suffix);
            }
        }
        return urls;
    }

    @Override
    protected boolean saveNewUrlList(CrawlUrl crawlUrl, List<String> urlList) throws Exception {
        // 生成书籍首页url
        String host = UrlUtil.getDomain(crawlUrl.getUrl());
        String prefix = "http://" + host;
        for (String url : urlList) {
            log.info("章节url:{}", url);
            String chapterUrl = prefix + url;
            insert(crawlUrl.getPolicyId(), crawlUrl.getReferId(), chapterUrl, ChannelEnum.CHAPTER);
        }
        return true;
    }
}
